package com.powernode.crm.activity.service.impl;

import com.powernode.crm.activity.mapper.ActivityMapper;
import com.powernode.crm.activity.model.Activity;
import com.powernode.crm.activity.service.ActivityService;
import com.powernode.crm.transaction.mapper.TranMapper;
import com.powernode.crm.transaction.model.Tran;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private TranMapper tranMapper;

    @Override
    public Activity queryById(String id) {
        return activityMapper.selectById(id);
    }

    @Override
    public List<Activity> queryPage(String name, String owner, String startDate, String endDate, Integer pageNum, Integer pageSize) {
        return activityMapper.selectPage(name,owner,startDate,endDate,pageNum,pageSize);
    }

    @Override
    public Integer queryListCount(String name, String owner, String startDate, String endDate) {
        return activityMapper.selectListCount(name,owner,startDate,endDate);
    }

    @Override
    public int addActivity(Activity activity) {
        return activityMapper.insert(activity);
    }

    @Override
    public int modifyActivity(Activity activity) {
        return activityMapper.updateByPrimaryKeySelective(activity);
    }

    /**
     * 批量删除
     * @param ids  id数组
     * @return  影响的行数
     */
    @Override
    public int removeActivity(String[] ids) {
        /*1 通过遍历数组 一个一个删 */
        /*2 使用动态sql  批量删除*/
        return activityMapper.deleteByIds(ids);
    }

    @Override
    public int saveActivityList(List<Activity> activities) {
        return activityMapper.insertActivityList(activities);
    }

    @Override
    public List<Activity> queryAll() {
        return activityMapper.selectAll();
    }

    @Override
    public List<Activity> queryByIds(String[] ids) {
        return activityMapper.selectByIds(ids);
    }

    @Override
    public List<Activity> queryByName(String name) {
        return activityMapper.selectByName(name);
    }


    public List<Tran> queryPages(String owner, String activityName, String customerName, String stage, String type, String source, String contactsFullName, Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("owner", owner);
        params.put("activityName", activityName);
        params.put("customerName", customerName);
        params.put("stage", stage);
        params.put("type", type);
        params.put("source", source);
        params.put("contactsFullName", contactsFullName);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        List<Tran> trans = tranMapper.selectByParams(params);
        return trans;
    }

    @Override
    public int queryCount(String owner, String activityName, String customerName, String stage, String type, String source, String contactsFullName) {
        Map<String, Object> params = new HashMap<>();
        params.put("owner", owner);
        params.put("activityName", activityName);
        params.put("customerName", customerName);
        params.put("stage", stage);
        params.put("type", type);
        params.put("source", source);
        params.put("contactsFullName", contactsFullName);
        Integer rows=tranMapper.selectCountByParams(params);
        return rows;
    }
}
